package com.tiandang.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import tiandang.core.exception.UnauthorizedException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@Service
public class RequestInterceptor extends HandlerInterceptorAdapter {

  // @Autowired
  // protected Session session;

  @Autowired
  protected SessionMap sessionMap;

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    String clientToken = getClientToken(request);

    if (!sessionMap.isLogin(clientToken)) {
      throw new UnauthorizedException();
    }

    return super.preHandle(request, response, handler);
  }

  private String getClientToken(HttpServletRequest request) {
    String clientToken = "";
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
      String key = (String) headerNames.nextElement();
      String value = request.getHeader(key);
      if (key.equals("token")) {
        clientToken = value;
      }
    }
    return clientToken;
  }

}
